package com.luocj.muke.demo.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.luocj.muke.demo.R;
import com.luocj.muke.demo.fragment.FragmentA;
import com.luocj.muke.demo.fragment.HomeFragment;
import com.luocj.muke.demo.fragment.MineFragment;
import com.luocj.muke.demo.fragment.NewsFragment;

import java.util.ArrayList;

public class ViewpagerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        initView();
    }

    private void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new FragmentA());
        fragments.add(new NewsFragment());
        fragments.add(new MineFragment());
        ViewPager viewpager = findViewById(R.id.viewpager);
        MyPagerAdapter adapter = new MyPagerAdapter(fragments,getSupportFragmentManager());
        viewpager.setAdapter(adapter);
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        private ArrayList<Fragment> mFragments = new ArrayList<>();
        public MyPagerAdapter(ArrayList<Fragment> fragments, FragmentManager fm) {
            super(fm);
            this.mFragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
